//
// Created by ZLY on 2025/5/9.
//

#include "effect.h"
#include "../core/scene.h"

void Effect::update(float dt) {
    ObjectWorld::update(dt);
    checkFinished();
}

void Effect::clean() {
    ObjectWorld::clean();
    if (nextObject_) {// next_object 添加到场景不能删除
        nextObject_->clean();
        delete nextObject_;
        nextObject_ = nullptr;
    }
}

void Effect::checkFinished() {
    if (sprite_->getFinished()) {
        need_remove_ = true;
        if (nextObject_) {
            game_.getCurrentScene()->safeAddChild(nextObject_);
            nextObject_ = nullptr;
        }
    }
}

Effect *Effect::addEffectChild(Object *parent, const std::string &file_path, glm::vec2 pos, float scale,
                               ObjectWorld *next_object) {
    auto effect = new Effect();
    effect->init();
    effect->sprite_ = SpriteAnim::addSpriteAnimChild(effect, file_path, scale);
    effect->sprite_->setLoop(false);
    effect->setPosition(pos);
    effect->setNextObject(next_object);
    if (parent) {
        parent->addChild(effect);
    }
    return effect;
}
